package com.ywj.androidtest;



import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class Main extends Activity {
	Button btn;
	Intent intent;
	ListView lv;
	SQLiteDatabase db;
	Cursor cursor;
	MySqliteHelper helper;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		btn=(Button) findViewById(R.id.button_create);
		lv=(ListView) findViewById(R.id.listView1);
		helper=DbManager.getIntance(this);
		btn.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View arg0) {
				intent=new Intent(Main.this,Create.class);
				startActivity(intent);
			}
		});
		SQLiteDatabase db=helper.getWritableDatabase();
		String sql="select _id,title,content,createdata from usercontent order by _id;";
		cursor=db.rawQuery(sql, null);
		SimpleCursorAdapter adapter=new SimpleCursorAdapter(this,R.layout.item_style,cursor,
				new String[]{"_id","title","content","createdata"}, 
				new int[]{R.id.textView1,R.id.textView2,R.id.textView3,R.id.textView4},
				SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
		lv.setAdapter(adapter);
		lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				TextView tv1=(TextView)arg1. findViewById(R.id.textView1);
				String id=tv1.getText().toString();
				Intent intent=new Intent(Main.this,Content.class);
				intent.putExtra("id",id);
				startActivity(intent);	
			}
		});
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
